package com.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.mapper.RoleMapper;
import com.boot.mapper.RolePermissionMapper;
import com.boot.entity.Role;
import com.boot.entity.RolePermssion;
import com.boot.service.RoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper,Role> implements RoleService {

    private final RoleMapper roleMapper;

    private final RolePermissionMapper rolePermissionMapper;

    public void deleteRole(Long roleId) {
        roleMapper.deleteById(roleId);
    }

    /**
     * 添加角色-权限之间关系
     * @param roleId
     * @param permissionIds
     */
    public void correlationPermissions(Long roleId, Long... permissionIds) {
        if(permissionIds != null && permissionIds.length > 0) {
            for (Long permissionId : permissionIds) {
                RolePermssion rolePermssion = new RolePermssion();
                rolePermssion.setRoleId(roleId);
                rolePermssion.setPermissionId(permissionId);
                rolePermissionMapper.insert(rolePermssion);
            }
        }

    }

    /**
     * 移除角色-权限之间关系
     * @param roleId
     * @param permissionIds
     */
    public void uncorrelationPermissions(Long roleId, Long... permissionIds) {
        if(permissionIds!=null && permissionIds.length>0){
            for (Long permissionId : permissionIds) {
                QueryWrapper<RolePermssion> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("role_id",roleId);
                queryWrapper.eq("permission_id",permissionId);
                rolePermissionMapper.delete(queryWrapper);
            }
        }
    }

}
